import React , {Component}from 'react';
import { Modal, Button } from 'antd';
import InputBox from '../../common/InputBox.js'
import Select from '../../common/Select.js'
import  Confirm from '../../common/Confirm.js'
import GetRolesMutation from "./mutation/GetRolesMutation"
import Relay from "react-relay";
import CreateUserMutation from "./mutation/CreateUserMutation"


export default class Addstaff extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            roles:[],
            departname:{},
            partid :0,
            departmentforchoose:[],
            companyhint:'',
            mal:'',
            connection:'',
            ratepayer:'',

        }
        this.Style = {};
        this.roles = [{text:"请选择职务",value:"0"}]
        this.corresponding = {}
        this.correspondingen = {}
        this.pid = {}
        this.i = 0
        this.j = 0
        this.k = 0
    }

    componentWillMount() {
        Relay.Store.commitUpdate(
            new GetRolesMutation({
                enid:'26'
            }),
            {
                onFailure: () => {
                },
                onSuccess: (response) => {
                    let errorcode = response.getRoles.getRoles.errorCode
                    if(errorcode ==0){
                        let roles = response.getRoles.getRoles.roles;
                        roles.map((context,index)=>{
                            let name = context.name
                            this.roles.push({"text":name,value:(index+1)+''})
                            this.corresponding[name]=context.id.rid;
                            this.correspondingen[name]=context.id.enid;
                            this.pid[name] = context.pid
                        });

                        this.setState({
                            roles : this.roles
                        })
                    }
                }
            }
        )
        
        
        this.Style.addstaffview={
            textAlign:"center",
            display:"block",
            border:"3px solid green"
        }
        this.Style.addstaffview.border = "3px solid red";
        this.Style.addstaffview.width = 400;
        this.Style.addstaffview.height = 200;
        this.Style.addstaffview.fontSize = 30;
        this.Style.headerview={
            width:"634px",
            height:"44px",
            paddingLeft:"18px",
            lineHeight:"44px",
            display:"block",
            background:"#ccc",
            marginBottom:49

        }
    }

    state = { visible: false }
    showModal = () => {
        this.setState({
            visible: true,
        });
    }
    handleOk = (e) => {
        console.log(e);
        this.setState({
            visible: false,
        });
    }
    handleCancel = (e) => {
        console.log(e);
        this.setState({
            visible: false,
        });
    }
    handleClick(){
        //console.warn(111)
    }

    addworker(){
        // if(this.refs.phone.state.text==''){
        //     return
        // }

        // let test2 = this.refs._enterpriseCode.state.text;
        // let reg = /^([0-9A-Z]){8}[-][0-9]$/;
        // if(reg.test(test2)==false){
        //     return;
        // }
        let companyname1 = this.refs._enterpriseName.state.text;
        let taxpayerNo1 = this.refs._taxpayerNo.state.text;
        let telephone1 = this.refs._telephone.state.text;

        let re_taxpayerNo = /^([0-9]|[a-z]|[A-Z]){6,12}$/;
        let re_telephone =/^(((13[0-9]{1})|(17[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;

        if(re_taxpayerNo.test(taxpayerNo1)){
            this.i = 1;
        }else {
            this.i = 0;
        }
        if(re_telephone.test(telephone1)){
            this.j = 1;
        }else {
            this.j = 0;
        }
        if(companyname1!=""){
            this.k = 1;
        }else {
            this.k = 0;
        }



        if(this.i+this.j+this.k==3){

            console.warn(this.correspondingen[this.refs.roles.state.value]);
            Relay.Store.commitUpdate(

                new CreateUserMutation({
                    enterpriseId: '26',
                    name: this.refs._enterpriseName.state.text,
                    phone: this.refs._telephone.state.text,
                    password: this.refs._taxpayerNo.state.text,
                    email: this.refs._enterpriseCode.state.text,
                    depId: this.props.deparmentc[this.refs.partname.state.value],
                    roleId: this.corresponding[this.refs.roles.state.value],
                    roleName: this.refs.roles.state.value,
                    pid: this.pid[this.refs.roles.state.value],
                    roEnId:this.correspondingen[this.refs.roles.state.value]
                }),
                {
                    onFailure: () => {
                    },
                    onSuccess: (response) => {
                        let name = this.refs.partname.state.value;
                        let inde = this.state.departmentforchoose.indexOf(name)
                        this.props.onchose(name,inde);
                    }
                }
            )

        }else {
            return;
        }


        this.setState({
            visible:false
        });
    }


    bluron(type){
        switch (type){

            case 'companyhint':

                let companyname1 = this.refs._enterpriseName.state.text;

                this.companyname = companyname1;

                if(this.companyname==""){
                    this.setState({
                        companyhint:"姓名不能为空"
                    });
                }else{
                    this.setState({
                        companyhint:""
                    });
                }
                return;

            case 'mal':
                let enterpriseCode1 = this.refs._enterpriseCode.state.text;
                let re_enterpriseCode = /^([0-9]|[a-z]|[A-Z]){3,15}@([0-9]|[a-z]|[A-Z]){1,5}[\.]([0-9]|[a-z]|[A-Z]){2,5}$/

                if(re_enterpriseCode.test(enterpriseCode1)){
                    this.enterpriseCode = enterpriseCode1;

                    this.setState({
                        mal:""
                    });
                }else if(enterpriseCode1==""){
                    this.setState({
                        mal:""
                    });
                }else{
                    this.setState({
                        mal:"邮箱格式错误，请重新输入"
                    });
                }
                return;

            case 'connection':
                let telephone1 = this.refs._telephone.state.text;
                let re_telephone =/^(((13[0-9]{1})|(17[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;

                let telephone;
                if(re_telephone.test(telephone1)){
                    telephone = telephone1;
                    this.setState({
                        connection:""
                    });
                }else if(telephone1==""){
                    this.setState({
                        connection:""
                    });
                }else {
                    this.setState({
                        connection:"手机号格式错误，请重新输入"
                    });
                }
                return;

            case 'ratepayer':
                let taxpayerNo1 = this.refs._taxpayerNo.state.text;
                let re_taxpayerNo = /^([0-9]|[a-z]|[A-Z]){6,12}$/;
                let taxpayerNo;
                if(re_taxpayerNo.test(taxpayerNo1)){
                    taxpayerNo = taxpayerNo1;
                    this.setState({
                        ratepayer:""
                    });
                }else if(taxpayerNo1==""){
                    this.setState({
                        ratepayer:""
                    });
                }else {
                    this.setState({
                        ratepayer:"密码格式错误，请重新输入"
                    });
                }
                return;

        }
    }



    render(){
        return (
            <Modal visible={this.state.visible} onOk={this.handleOk} onCancel={this.handleCancel} style={{width:"634px",height:"565px",display:"block"}} title={"添加员工"}>
                <span style={{position:"absolute",display:"block",left:460,top:7}}>（用于账号登陆）</span>
                <InputBox defaultvalute="请输入手机号" titletext="手机号" space="40px" style={{width:312,height:30,borderColor:"#efefef",color:"#999",paddingRight:137}} ref={'_telephone'} bluron={this.bluron.bind(this,'connection')}/>
                <span style={{color:"#ff6f64",position:"absolute",left:"120px",top:"33px"}}>{this.state.connection}</span>
                <InputBox defaultvalute="请输入姓名" titletext="姓名" space="40px" style={{width:312,height:30,borderColor:"#efefef",color:"#999",marginTop:"20px",paddingRight:137}} ref={"_enterpriseName"} bluron={this.bluron.bind(this,'companyhint')}/>
                <span style={{color:"#ff6f64",position:"absolute",left:"120px",top:"86px"}}>{this.state.companyhint}</span>
                <InputBox defaultvalute="请输入密码，6-12位的字母或数字组成" titletext="密码" space="40px" style={{width:312,height:30,borderColor:"#efefef",color:"#999",marginTop:20,paddingRight:137}} ref={"_taxpayerNo"} bluron={this.bluron.bind(this,'ratepayer')}/>
                <span style={{color:"#ff6f64",position:"absolute",left:"240px",top:"139px"}}>{this.state.ratepayer}</span>
                <label style={{display:"block",marginLeft:136,marginTop:12}}><input type="checkbox" style={{display:"inlineBlock",marginRight:5}}/>设为默认密码</label>
                <Confirm tcontext="添加员工" style={{width:265,height:43,display:"block",color:"#fff",fontSize:"16px",marginTop:60,marginLeft:158}} touch={this.addworker.bind(this)}/>
            </Modal>
            )
    }
}


